package my.sample.quartz;

import static org.quartz.DateBuilder.futureDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OneTimeJob implements Job {

	/**
	 * @param args
	 * @throws SchedulerException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws SchedulerException, InterruptedException {
		// TODO Auto-generated method stub
		
		Logger log = LoggerFactory.getLogger(OneTimeJob.class);
		
		SchedulerFactory fact = new StdSchedulerFactory();
		Scheduler scheduler = fact.getScheduler();
		System.out.println("scheduler name: " + scheduler.getSchedulerName());
		scheduler.start();
		
		scheduler.getCurrentlyExecutingJobs();
		
		// job
		JobDetail job = newJob(OneTimeJob.class).withIdentity("myFirstJob", "myGroup").build();
		
		
		
/*		// trigger
		CronTrigger trigger = newTrigger()
				.withIdentity("trigger", "triggerGroup")
				.startAt(start)
				.endAt(stop)
				.withSchedule(cronSchedule("0/10 * * * * ?")).build();*/
				
		SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger", "triggerGroup").startAt(futureDate(1, IntervalUnit.MINUTE)).build();
//		SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger", "triggerGroup").startAt(futureDate(2, IntervalUnit.MINUTE)).forJob(job).build();
		
		/*CronTrigger trigger = (SimpleTrigger) newTrigger() 
			    .withIdentity("trigger5", "group1")
			    .startAt(futureDate(5, IntervalUnit.MINUTE)) // use DateBuilder to create a date in the future
			    .forJob(myJobKey) // identify job with its JobKey
			    .build();*/
		
		
//        CronTrigger trigger1 = newTrigger().withIdentity("secondTrigger", "triggerGroup").withSchedule(cronSchedule("0/5 * * * * ?")).build();
		
		//trigger own
		
        
		log.info("scheduling job");
		scheduler.scheduleJob(job, trigger);
		log.info("sch started");
	}

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("job execute at : " + new Date());
	}

}
